package com.bt.lms.dao.servise.impl;

import com.bt.lms.bo.FinishedTeachingArrange;
import com.bt.lms.bo.NoStartTeachingArrange;
import com.bt.lms.bo.ProcessTeachingArrange;
import com.bt.lms.constant.BOConstant;
import com.bt.lms.entity.ClassInfo;
import com.bt.lms.entity.CourseInfo;
import com.bt.lms.entity.TeacherBasicInfo;
import com.bt.lms.entity.TeachingArrangeInfo;
import com.bt.lms.dao.servise.TeachingArrangeDao;
import com.bt.lms.entity.page.Page;
import com.bt.lms.utils.JDBCUtils;

import java.sql.*;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * 教学安排Dao实现类
 */
public class TeachingArrangeDaoImpl implements TeachingArrangeDao {


    @Override
    public int insert(String sql, TeachingArrangeInfo teachingArrangeInfo) throws ClassNotFoundException, SQLException {

//        CourseInfo courseInfo = teachingArrangeInfo.getCourseInfo();
//        String courseName = courseInfo.getName();//课程名

        String course = teachingArrangeInfo.getCourse();
        String teacher = teachingArrangeInfo.getTeacher();
        String setClass = teachingArrangeInfo.getSetClass();


//        TeacherBasicInfo teacherBasicInfo = teachingArrangeInfo.getTeacherBasicInfo();//教师信息
//        String name = teacherBasicInfo.getName();//教师名
//        ClassInfo classInfo = teachingArrangeInfo.getClassInfo();//班级信息
//        String className = classInfo.getName();//班级名
        Integer teachingWay = teachingArrangeInfo.getTeachingWay();//教学方式
        LocalDate startCourseTime = teachingArrangeInfo.getStartCourseTime();//开课时间
        LocalDate finishCourseTime = teachingArrangeInfo.getFinishCourseTime();//结课时间
        String courseAim = teachingArrangeInfo.getCourseAim();//课程目标


        //JDBC
        Connection jdbc = JDBCUtils.JDBC();
        PreparedStatement prepareStatement = jdbc.prepareStatement(sql);


        //预编译
        prepareStatement.setString(1, course);
        prepareStatement.setString(2, teacher);
        prepareStatement.setString(3, setClass);
        prepareStatement.setInt(4, teachingWay);
        prepareStatement.setDate(5, Date.valueOf(startCourseTime));
        prepareStatement.setDate(6, Date.valueOf(finishCourseTime));
        prepareStatement.setString(7, courseAim);


        //执行sql语句
        int i = prepareStatement.executeUpdate();


        return 0;
    }


    @Override
    public boolean edit(String sql, TeachingArrangeInfo teachingArrangeInfo, Long teachingArrangeID) throws ClassNotFoundException, SQLException {


        CourseInfo courseInfo = teachingArrangeInfo.getCourseInfo();
        String courseName = courseInfo.getName();//课程名
        TeacherBasicInfo teacherBasicInfo = teachingArrangeInfo.getTeacherBasicInfo();//教师信息
        String name = teacherBasicInfo.getName();//教师名
        ClassInfo classInfo = teachingArrangeInfo.getClassInfo();//班级信息
        String className = classInfo.getName();//班级名
        Integer teachingWay = teachingArrangeInfo.getTeachingWay();//教学方式
        LocalDate startCourseTime = teachingArrangeInfo.getStartCourseTime();//开课时间
        LocalDate finishCourseTime = teachingArrangeInfo.getFinishCourseTime();//结课时间
        String courseAim = teachingArrangeInfo.getCourseAim();//课程目标


        //JDBC
        Connection jdbc = JDBCUtils.JDBC();
        PreparedStatement prepareStatement = jdbc.prepareStatement(sql);


        //预编译
        prepareStatement.setString(1, courseName);
        prepareStatement.setString(2, name);
        prepareStatement.setString(3, className);
        prepareStatement.setInt(4, teachingWay);
        prepareStatement.setDate(5, Date.valueOf(startCourseTime));
        prepareStatement.setDate(6, Date.valueOf(finishCourseTime));
        prepareStatement.setString(7, courseAim);
        prepareStatement.setLong(8, teachingArrangeID);


        //执行sql语句
        int i = prepareStatement.executeUpdate();


        return true;
    }


    @Override
    public boolean delete(String sql, Long teachingArrangeID) throws ClassNotFoundException, SQLException {


        //JDBC
        Connection jdbc = JDBCUtils.JDBC();
        PreparedStatement prepareStatement = jdbc.prepareStatement(sql);

        prepareStatement.setInt(1, BOConstant.DELETED_TRUE);
        prepareStatement.setLong(2, teachingArrangeID);


        //执行sql语句
        int i = prepareStatement.executeUpdate();


        return true;
    }


    public boolean affirmCourseStart(String sql, Long teachingArrangeID) throws ClassNotFoundException, SQLException {


        //JDBC
        Connection jdbc = JDBCUtils.JDBC();
        PreparedStatement prepareStatement = jdbc.prepareStatement(sql);

        prepareStatement.setInt(1, BOConstant.TEACHING_ARRANGE_INFO_STATUS_PUBLISHED);
        prepareStatement.setLong(2, teachingArrangeID);

        //执行sql语句
        int i = prepareStatement.executeUpdate();


        return true;


    }


    public TeachingArrangeInfo queryDetail(String sql, Long teachingArrangeID) throws ClassNotFoundException, SQLException {


        //教学安排信息
        TeachingArrangeInfo teachingArrangeInfo = new TeachingArrangeInfo();

        //JDBC
        Connection jdbc = JDBCUtils.JDBC();
        PreparedStatement prepareStatement = jdbc.prepareStatement(sql);
        prepareStatement.setLong(1, teachingArrangeID);


        ResultSet resultSet = prepareStatement.executeQuery();
        boolean next = resultSet.next();
        if (next) {

            String course_name = resultSet.getString("course_name");//课程名字
            String teaching_name = resultSet.getString("teaching_name");//教师名字
            String class_name = resultSet.getString("class_name");//班级名字

            int teaching_way = resultSet.getInt("teaching_way");//教学方式
            Date start_course_time = resultSet.getDate("start_course_time");//开课时间
            Date finish_course_time = resultSet.getDate("finish_course_time");//结课时间
            String course_aim = resultSet.getString("course_aim");//课程目标


            //带走
            CourseInfo courseInfo = new CourseInfo();
            courseInfo.setName(course_name);
            teachingArrangeInfo.setCourseInfo(courseInfo);

            TeacherBasicInfo teacherBasicInfo = new TeacherBasicInfo();
            teacherBasicInfo.setName(teaching_name);
            teachingArrangeInfo.setTeacherBasicInfo(teacherBasicInfo);

            ClassInfo classInfo = new ClassInfo();
            classInfo.setName(class_name);
            teachingArrangeInfo.setClassInfo(classInfo);


            teachingArrangeInfo.setTeachingWay(teaching_way);
            teachingArrangeInfo.setStartCourseTime(start_course_time.toLocalDate());
            teachingArrangeInfo.setFinishCourseTime(finish_course_time.toLocalDate());
            teachingArrangeInfo.setCourseAim(course_aim);


        }


        return teachingArrangeInfo;


    }

    @Override
    public int insert1(String sql, TeachingArrangeInfo teachingArrangeInfo) throws SQLException {


//        CourseInfo courseInfo = teachingArrangeInfo.getCourseInfo();
//        String courseName = courseInfo.getName();//课程名
//
//        TeacherBasicInfo teacherBasicInfo = teachingArrangeInfo.getTeacherBasicInfo();//教师信息
//        String name = teacherBasicInfo.getName();//教师名
//        ClassInfo classInfo = teachingArrangeInfo.getClassInfo();//班级信息
//        String className = classInfo.getName();//班级名

        String course = teachingArrangeInfo.getCourse();//课程名
        String teacher = teachingArrangeInfo.getTeacher();//教师名
        String setClass = teachingArrangeInfo.getSetClass();//班级名
        Integer teachingWay = teachingArrangeInfo.getTeachingWay();//教学方式
        LocalDate startCourseTime = teachingArrangeInfo.getStartCourseTime();//开课时间
        LocalDate finishCourseTime = teachingArrangeInfo.getFinishCourseTime();//结课时间
        String courseAim = teachingArrangeInfo.getCourseAim();//课程目标
        Integer status = teachingArrangeInfo.getStatus();//教学安排状态


        //JDBC
        Connection jdbc = JDBCUtils.JDBC();
        PreparedStatement prepareStatement = jdbc.prepareStatement(sql);


        //预编译
        prepareStatement.setString(1, course);
        prepareStatement.setString(2, teacher);
        prepareStatement.setString(3, setClass);
        prepareStatement.setInt(4, teachingWay);
        prepareStatement.setDate(5, Date.valueOf(startCourseTime));
        prepareStatement.setDate(6, Date.valueOf(finishCourseTime));
        prepareStatement.setString(7, courseAim);
        prepareStatement.setInt(8, status);


        int i = prepareStatement.executeUpdate();


        return 0;
    }

    @Override
    public Page<TeachingArrangeInfo> pageQueryNoStart(String sql, Page<NoStartTeachingArrange> page) throws SQLException {

        Connection jdbc = JDBCUtils.JDBC();
        PreparedStatement prepareStatement = jdbc.prepareStatement(sql);

        Integer currentPage = page.getCurrent();//当前页
        Integer sizePage = page.getSize();//每页多少条

        //预编译
        prepareStatement.setInt(1, currentPage);
        prepareStatement.setInt(2, sizePage);

        //执行
        ResultSet resultSet = prepareStatement.executeQuery();

        Page<TeachingArrangeInfo> teachingArrangeInfoPage = new Page<>();

        //教学安排信息集合
        List<TeachingArrangeInfo> teachingArrangeInfos = new ArrayList<>();

        teachingArrangeInfoPage.setRecords(teachingArrangeInfos);

        while (resultSet.next()) {
            int id = resultSet.getInt("id"); //id
            String course_name = resultSet.getString("course_name");//课程名
            String teaching_name = resultSet.getString("teaching_name");//教师名
            String class_name = resultSet.getString("class_name");//班级名
            int teaching_way = resultSet.getInt("teaching_way");//教学方式
            Date start_course_time = resultSet.getDate("start_course_time");//开课时间
            Date finish_course_time = resultSet.getDate("finish_course_time");//结课时间
            String course_aim = resultSet.getString("course_aim");//课程目标

            TeachingArrangeInfo teachingArrangeInfo = new TeachingArrangeInfo();
            teachingArrangeInfo.setId((long) id);
            teachingArrangeInfo.setCourse(course_name);
            teachingArrangeInfo.setTeacher(teaching_name);
            teachingArrangeInfo.setSetClass(class_name);
            teachingArrangeInfo.setTeachingWay(teaching_way);
            teachingArrangeInfo.setStartCourseTime(start_course_time.toLocalDate());
            teachingArrangeInfo.setFinishCourseTime(finish_course_time.toLocalDate());
            teachingArrangeInfo.setCourseAim(course_aim);
            teachingArrangeInfos.add(teachingArrangeInfo);
        }

        return teachingArrangeInfoPage;
    }

    @Override
    public Page<TeachingArrangeInfo> pageQueryProcess(String sql, Page<ProcessTeachingArrange> page) throws SQLException {

        Connection jdbc = JDBCUtils.JDBC();
        PreparedStatement prepareStatement = jdbc.prepareStatement(sql);

        Integer currentPage = page.getCurrent();//当前页
        Integer sizePage = page.getSize();//每页多少条

        //预编译
        prepareStatement.setInt(1, currentPage);
        prepareStatement.setInt(2, sizePage);

        //执行
        ResultSet resultSet = prepareStatement.executeQuery();

        Page<TeachingArrangeInfo> teachingArrangeInfoPage = new Page<>();

        //教学安排信息集合
        List<TeachingArrangeInfo> teachingArrangeInfos = new ArrayList<>();

        teachingArrangeInfoPage.setRecords(teachingArrangeInfos);


        while (resultSet.next()) {
            int id = resultSet.getInt("id"); //id
            String course_name = resultSet.getString("course_name");//课程名
            String teaching_name = resultSet.getString("teaching_name");//教师名
            String class_name = resultSet.getString("class_name");//班级名
            int teaching_way = resultSet.getInt("teaching_way");//教学方式
            Date start_course_time = resultSet.getDate("start_course_time");//开课时间
            Date finish_course_time = resultSet.getDate("finish_course_time");//结课时间
            String course_aim = resultSet.getString("course_aim");//课程目标

            TeachingArrangeInfo teachingArrangeInfo = new TeachingArrangeInfo();
            teachingArrangeInfo.setId((long) id);
            teachingArrangeInfo.setCourse(course_name);
            teachingArrangeInfo.setTeacher(teaching_name);
            teachingArrangeInfo.setSetClass(class_name);
            teachingArrangeInfo.setTeachingWay(teaching_way);
            teachingArrangeInfo.setStartCourseTime(start_course_time.toLocalDate());
            teachingArrangeInfo.setFinishCourseTime(finish_course_time.toLocalDate());
            teachingArrangeInfo.setCourseAim(course_aim);
            teachingArrangeInfos.add(teachingArrangeInfo);
        }

        return teachingArrangeInfoPage;
    }

    @Override
    public Page<TeachingArrangeInfo> pageQueryFinish(String sql, Page<FinishedTeachingArrange> page) throws SQLException {

        Connection jdbc = JDBCUtils.JDBC();
        PreparedStatement prepareStatement = jdbc.prepareStatement(sql);

        Integer currentPage = page.getCurrent();//当前页
        Integer sizePage = page.getSize();//每页多少条

        //预编译
        prepareStatement.setInt(1, currentPage);
        prepareStatement.setInt(2, sizePage);

        //执行
        ResultSet resultSet = prepareStatement.executeQuery();

        Page<TeachingArrangeInfo> teachingArrangeInfoPage = new Page<>();

        //教学安排信息集合
        List<TeachingArrangeInfo> teachingArrangeInfos = new ArrayList<>();

        teachingArrangeInfoPage.setRecords(teachingArrangeInfos);


        while (resultSet.next()) {
            int id = resultSet.getInt("id"); //id
            String course_name = resultSet.getString("course_name");//课程名
            String teaching_name = resultSet.getString("teaching_name");//教师名
            String class_name = resultSet.getString("class_name");//班级名
            int teaching_way = resultSet.getInt("teaching_way");//教学方式
            Date start_course_time = resultSet.getDate("start_course_time");//开课时间
            Date finish_course_time = resultSet.getDate("finish_course_time");//结课时间
            String course_aim = resultSet.getString("course_aim");//课程目标

            TeachingArrangeInfo teachingArrangeInfo = new TeachingArrangeInfo();
            teachingArrangeInfo.setId((long) id);
            teachingArrangeInfo.setCourse(course_name);
            teachingArrangeInfo.setTeacher(teaching_name);
            teachingArrangeInfo.setSetClass(class_name);
            teachingArrangeInfo.setTeachingWay(teaching_way);
            teachingArrangeInfo.setStartCourseTime(start_course_time.toLocalDate());
            teachingArrangeInfo.setFinishCourseTime(finish_course_time.toLocalDate());
            teachingArrangeInfo.setCourseAim(course_aim);
            teachingArrangeInfos.add(teachingArrangeInfo);
        }

        return teachingArrangeInfoPage;
    }


}



